package com.example.restatr.Atcivity.Park;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.Park.Bean.CarMileageBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CarMileageAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private RecyclerView mileageRV;
    private List<CarMileageBean.RowsBean> rowsBeans = new ArrayList<>();
    private PopupWindow popupWindow;
    private int pos;
    private LinearLayout Null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c_l_l_c_at);
        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        mileageRV = findViewById(R.id.mileageRV);
        Null = findViewById(R.id.Null);
        Tools.SetBarTitle(tb, barTitle, "车辆里程信息", search, CarMileageAt.this,false);
    }

    private void GetMileage() {
        new Thread(() -> {
            rowsBeans.clear();
            rowsBeans.addAll( new Gson().fromJson(Tools.Get("/prod-api/api/park/car/consumption?plateNo="
                            + getIntent().getStringExtra("plateNo"), MainActivity.sp.getString("token", ""))
                    ,CarMileageBean.class)
                    .getRows());
            runOnUiThread(() -> {
                if (rowsBeans.size() > 0) {
                    mileageRV.setVisibility(View.VISIBLE);
                    Null.setVisibility(View.GONE);
                    mileageRV.setAdapter(new RvAdapter());
                } else {
                    mileageRV.setVisibility(View.GONE);
                    Null.setVisibility(View.VISIBLE);
                }
            });
        }).start();
    }

    public void ppWindowClick(View view) {
        switch (view.getId()) {
            case R.id.edit:
                Intent intent = new Intent(CarMileageAt.this, AddMileageAt.class);
                intent.putExtra("id", rowsBeans.get(pos).getId());
                intent.putExtra("date", rowsBeans.get(pos).getTravelDate());
                intent.putExtra("distance", rowsBeans.get(pos).getTravelDistance());
                intent.putExtra("gasFill", rowsBeans.get(pos).getGasFilling());
                intent.putExtra("amount", rowsBeans.get(pos).getAmount());
                startActivity(intent);
                break;
            case R.id.delete:
                new Thread(() -> {
                    String token = Tools.Delete("/prod-api/api/park/car/consumption/" + rowsBeans.get(pos).getId(), MainActivity.sp.getString("token", ""));
                    runOnUiThread(() -> {
                        try {
                            JSONObject jx = new JSONObject(token);
                            int code = jx.getInt("code");
                            if (code == 200) {
                                Toast.makeText(this, "删除成功！", Toast.LENGTH_SHORT).show();
                                GetMileage();
                                popupWindow.dismiss();
                            } else {
                                Toast.makeText(this, "删除失败！", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    });
                }).start();
                break;
        }
        popupWindow.dismiss();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.AddMileage:
                Intent intent = new Intent(CarMileageAt.this, AddMileageAt.class);
                intent.putExtra("plateNo", getIntent().getStringExtra("plateNo"));
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        GetMileage();
    }

    class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvViewHolder> {

        @NonNull
        @Override
        public RvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new RvViewHolder(getLayoutInflater().inflate(R.layout.mileage_item_layout, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull RvViewHolder holder, int position) {
            holder.date.setText(rowsBeans.get(position).getTravelDate());
            holder.distance.setText(rowsBeans.get(position).getTravelDistance() + "km");
            holder.gasFilling.setText(rowsBeans.get(position).getGasFilling() + "升");
            holder.amount.setText(rowsBeans.get(position).getAmount() + "元");
            holder.RL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    View inflate = getLayoutInflater().inflate(R.layout.log_layout, null);
                    popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                    popupWindow.setOutsideTouchable(true);
                    popupWindow.showAsDropDown(holder.RL, 0, 0, Gravity.CLIP_HORIZONTAL);
                    pos = position;
                }
            });
        }

        @Override
        public int getItemCount() {
            return rowsBeans.size();
        }

        class RvViewHolder extends RecyclerView.ViewHolder {
            private TextView date;
            private TextView distance;
            private TextView gasFilling;
            private TextView amount;
            private RelativeLayout RL;

            public RvViewHolder(@NonNull View itemView) {
                super(itemView);
                date = itemView.findViewById(R.id.date);
                distance = itemView.findViewById(R.id.distance);
                gasFilling = itemView.findViewById(R.id.gasFilling);
                amount = itemView.findViewById(R.id.amount);
                RL = itemView.findViewById(R.id.RL);

            }
        }
    }
}